<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="UTF-8">
  <meta
    name="viewport"
    content="width=device-width, initial-scale=1.0"
  >
  <title>打印文件</title>
</head>

<body>
  <iframe
    id="pdfFrame"
    style="width: 100vw; height: 100vh; border: none;"
  ></iframe>

  <script>
    function getQueryParam (name) {
      const urlParams = new URLSearchParams(window.location.search);
      return urlParams.get(name);
    }

    const pdfUrl = getQueryParam("file");
    if (pdfUrl) {
      const iframe = document.getElementById("pdfFrame");
      iframe.src = pdfUrl;

      // **轮询检查 PDF 是否加载完成**
      let checkInterval = setInterval(() => {
        try {
          if (iframe.contentDocument.readyState === "complete") {
            clearInterval(checkInterval); // 停止轮询
            setTimeout(() => {
              iframe.contentWindow.print();
            }, 500); // 适当延迟
          }
        } catch (e) {
          console.warn("PDF 仍在加载...");
        }
      }, 500);
    } else {
      alert("未找到 PDF 文件");
    }
  </script>
</body>

</html>